package com.lsh.controller;

import com.lsh.annotation.LoginRequired;
import com.lsh.service.impl.UserServiceImpl;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import java.util.Map;

@RequestMapping("/user")
@Controller
public class UserController {
    @Resource
    UserServiceImpl userService;

    @LoginRequired
    @GetMapping("/setting")
    public String toSetting(){
        return "site/setting";
    }
    @LoginRequired
    @GetMapping("/profile")
    public String toProfile(){
        return "site/profile";
    }
    @LoginRequired
    @PostMapping("/upassword")
    public String updatePassword(String oldPassword, String newPassword1, String newPassword2,Model model){
        Map<String, Object> map = userService.updatePassword(oldPassword, newPassword1,newPassword2);
        if(map.get("success")!=null){
            return "/site/login";
        }else {
            model.addAttribute("oldMsg",map.get("oldPasswordMsg"));
            model.addAttribute("newMsg",map.get("newPasswordMsg"));
            model.addAttribute("newMsg2",map.get("newPasswordMsg2"));
            model.addAttribute("oldPass",oldPassword);
            model.addAttribute("newPass1",newPassword1);
            model.addAttribute("newPass2",newPassword2);
            return "site/setting";
        }
    }
}
